import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  UseGuards,
  ParseIntPipe,
  DefaultValuePipe,
  Query,
  Request,
} from '@nestjs/common';

import { UserService } from 'src/service/user.service';
import { CreateUserDto } from 'src/dto/create-user.dto';
import { UpdateUserDto } from 'src/dto/update-user.dto';
import { AdminGuard } from 'src/guards/admin.guard';

@Controller('admin/user')
@UseGuards(AdminGuard)
export class UserController {
  constructor(private readonly userService: UserService) {}

  @Post()
  create(@Body() createUserDto: CreateUserDto) {
    return this.userService.create(createUserDto);
  }

  @Get()
  findAll(
    @Query('pageNum', new DefaultValuePipe(1), ParseIntPipe) pageNum: number,
    @Query('pageSize', new DefaultValuePipe(10), ParseIntPipe) pageSize: number,
    @Query('email') email: string,
    @Query('phone') phone: string,
  ) {
    return this.userService.findAll(pageNum, pageSize, phone);
  }

  @Get('/userInfo')
  async userInfo(@Query('uid') uid: string) {
    return this.userService.findOne(+uid); // 获取用户信息
  }

  @Get('/distribution')
  async getDistributionUser(
    @Query('uid') uid: string,
    @Query('pageNum', new DefaultValuePipe(1), ParseIntPipe) pageNum: number,
    @Query('pageSize', new DefaultValuePipe(10), ParseIntPipe) pageSize: number,
  ) {
    return this.userService.findDistributionUser(pageNum, pageSize, +uid);
  }

  @Post('/bindParent')
  async bindParent(@Query('uid') uid: string, @Body('code') code: string) {
    return this.userService.bindParent(+uid, code); // 获取用户信息
  }

  @Post('/updateUserInfo')
  async updateUserInfo(
    @Query('uid') uid: string,
    @Body() updateUserDto: UpdateUserDto,
  ) {
    return this.userService.updateUserInfo(+uid, updateUserDto); // 更新用户信息
  }

  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.userService.findOne(+id);
  }

  @Patch(':id')
  update(@Param('id') id: string, @Body() updateUserDto: UpdateUserDto) {
    return this.userService.update(+id, updateUserDto);
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.userService.remove(+id);
  }
}
